package com.gitee.feizns.dynamic.bean;

import java.util.function.Function;

/**
 * <b>指名映射函数的转换</b>
 * @author feizns
 * @since 2023/3/18
 */
public interface MapperConvertible<T extends MapperConvertible<T>> {

    /**
     * <b>将当前类型转换为目标类型</b>
     * <pre>{@code
     *  public class UserTest {
     *      public static Admin toAdmin(User user) { return new Admin(); }
     *      public static void main(String[] args) {
     *          User user = new User();
     *          Admin admin = user.map(UserTest::toAdmin);
     *          Admin admin = user.map(user -> new Admin(user));
     *          Admin admin = user.map(Admin::new);
     *      }
     *  }
     * }</pre>
     * @param mapper 从员类型映射到目标类型的映射函数
     * @return 返回函数结果
     * @param <U> 函数返回类型
     */
    @SuppressWarnings("unchecked")
    default <U> U map(Function<T, U> mapper) {
        return mapper.apply((T) this);
    }

}
